package etcd

import (
	"context"

	"github.com/go-kit/kit/sd/etcdv3"
	"github.com/go-kit/log"
)

// Register 服务注册
func Register(ctx context.Context, hosts []string, key string, value string, logger log.Logger) error {
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	client, err := etcdv3.NewClient(
		ctx,
		hosts,
		etcdv3.ClientOptions{},
	)
	if err != nil {
		return err
	}

	reg := etcdv3.NewRegistrar(client, etcdv3.Service{
		Key:   key,
		Value: value,
	}, logger)
	reg.Register()
	defer reg.Deregister()

	<-ctx.Done()
	return nil

}
